import { IUserMessageItem } from '@/models/messages';
import aepApiRequest from '../utils/aepApiRequest';

export type TMessageTyps = '1' | '2' | '3' | '4'; // 1:报警，2:故障, 3:通知, 4提醒

export interface IGetUserMessageProps {
  mac?: string;
  productKey?: string;
  sceneId?: number;
  msgType?: TMessageTyps;
}

export interface IUserMessage {
  alarmMsg: boolean;
  beginHour?: number;
  beginMinute?: number;
  deviceMsg: boolean;
  doNotDisturb?: boolean;
  endHour?: number;
  endMinute?: number;
  errorMsg: boolean;
  linkMsg: boolean;
  zone: number;
}

export interface IGetWxModeID {
  appId: string;
  productKey: string
  modelId: string
}
export async function getWxModeId(productKey: string) {
  const data = await aepApiRequest<IGetWxModeID>(`app/applets/getModelIdByProductKey?productKey=${productKey}`, {
    method: 'GET',
  });
  if (data.success) {
    return data.data?.modelId
  }
  return null;
}
export async function getUserMessage(params?: IGetUserMessageProps) {
  let url = 'app/user/notices';
  if (params && params.productKey && params.mac) {
    url = `${url}?productKey=${params.productKey}&mac=${params.mac}`;
  } else if (params && params.msgType) {
    url = `${url}?msgType=${params.msgType}`;
  }
  return aepApiRequest<IUserMessageItem[]>(url, {
    method: 'GET',
  });
}

export async function readAllUserMessage(params?: IGetUserMessageProps) {
  let url = 'app/user/notices/read';
  if (params && params.productKey && params.mac) {
    url = `${url}?pk=${params.productKey}&mac=${params.mac}`;
  } else if (params && params.sceneId) {
    url = `${url}?sceneId=${params.sceneId}`;
  }
  return aepApiRequest(url, {
    method: 'PUT',
  });
}

export async function getUserMessageSetting() {
  return aepApiRequest('app/user/notices/setting', {
    method: 'GET',
  });
}

export async function updateUserMessageSetting(params: IUserMessage) {
  return aepApiRequest('app/user/notices/setting', {
    method: 'PUT',
    body: params,
  });
}

export async function deleteUserMessages(arr: number[]) {
  return aepApiRequest('app/user/notices', {
    method: 'DELETE',
    body: arr,
  });
}

export async function readUserMessage(id: number) {
  return aepApiRequest(`app/user/notices/${id}/read`, {
    method: 'PUT',
  });
}

export async function addDeviceNoticeSetting(params: any) {
  return aepApiRequest('app/device/notice_settings', {
    method: 'POST',
    body: params,
  });
}

export async function getDeviceNoticeSetting(sno: string) {
  return aepApiRequest(`app/device/notice_settings/${sno}`, {
    method: 'GET',
  });
}

export async function updateDeviceNoticeSetting(params: any) {
  const { id } = params;
  return aepApiRequest(`app/device/notice_settings/${id}`, {
    method: 'PUT',
    body: params,
  });
}

export async function deleteNoticeSetting(id: number) {
  return aepApiRequest(`app/device/notice_settings/${id}`, {
    method: 'DELETE',
  });
}

export async function getDeviceContacts(sno: string) {
  return aepApiRequest(`app/device/device_contacts_setting/${sno}`, {
    method: 'GET',
  });
}

export async function setDeviceContacts(params: any) {
  return aepApiRequest(`app/device/device_contacts_setting/${params.sno}`, {
    method: 'POST',
    body: params,
  });
}

export async function setDeviceNoticeSettingBatch(params: any) {
  return aepApiRequest('/app/device/notice_settings/batch', {
    method: 'POST',
    body: params,
  });
}

export async function getProductNoticeSetting(pk: string) {
  const data: any = await aepApiRequest(`app/product/${pk}/conditions`, {
    method: 'GET',
  });
  data.productKey = pk;
  return data;
}

// 修改消息中心消息状态
// 默认是1待处理，2是已处理，接受接口会同步把这个状态变成2，不用再操作，3是拒绝，拒绝时要调修改消息状态的接口
export async function handleMessageStatus({ id, handleStatus }: { id: number; handleStatus: number }) {
  return aepApiRequest(`app/user/notices/${id}?handleStatus=${handleStatus}`, {
    method: 'PUT',
    body: {},
  });
}
